<template>
  <div style="margin-top:30px">
    <el-form ref="form" :rules="rules" :model="form" label-width="80px">
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="标题" prop="title" style="margin-right:15px">
              <el-input v-model="form.title" placeholder="请输入" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="内容" prop="content" style="margin-right:15px">
              <el-input type="textarea" :rows="3" v-model="form.content" placeholder="请输入" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="类型" prop="type" style="margin-right:15px">
              <el-select v-model="form.type" clearable placeholder="请选择">
                <el-option
                  v-for="(v,k) in dicts.siteDeploy"
                  :key="k"
                  :label="v"
                  :value="k"
                />
              </el-select>
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="序号" prop="idx" style="margin-right:15px">
              <el-input v-model="form.idx" placeholder="请输入" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="图片" prop="img" style="margin-right:15px">
              <el-upload
                v-model="form.img"
                list-type="picture-card"
                :action="uploadUrl"
                :show-file-list="false"
                :on-success="handleImgSuccess"
                :before-upload="beforeImgUpload"
                :headers="getAuthorization()"
              >
                <img v-if="form.img" :src="form.img" class="el-upload-list__item-thumbnail" width="100%" height="100%">
                <i v-else class="el-icon-plus avatar-uploader-icon" />
              </el-upload>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="funcCancel">取 消</el-button>
      <el-button type="primary" @click="funcSubmit">确 定</el-button>
    </div>
  </div>
</template>
<script>
  import Vue from 'vue'
  import 'xe-utils'
  import VXETable from 'vxe-table'
  import 'vxe-table/lib/index.css'
  import request from '@/utils/request'

  Vue.use(VXETable)
  export default {
    name: 'siteDeployAdd',
    data() {
      return {
        uploadUrl: this.BASE_API_URL + 'ntocc-tps-api/fileUpload',
        dicts: { siteDeploy: {}},
        form: {
          code: '',
          branchCode: '',
          title: '',
          content: '',
          idx:'',
          img: '',
          type: '',
          url: '',
          status: ''
        },
        rules: {
          title: [
            { required: true, message: '请输入标题', trigger: 'blur' },
            { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '请选择类型', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
    },
    methods: {
      // 提交
      funcSubmit() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            const data = this.form
            const rLoading = this.openLoading();
            request({ url: '/arpa-basic-api/siteDeploy', method: 'post', params: data }).then(response => {
              rLoading.close();
              this.$message({ message: response.msg, type: 'success' })
              this.$emit('fatherAddMethod')
            }).then(() => this.$refs['form'].resetFields())
          } else {
            return false
          }
        })
      },
      // 取消
      funcCancel() {
        this.$emit('fatherCancelMethod', 'add')
      },
      init(dicts) {
        this.dicts = dicts
        this.form = {
          title: '',
          img: '',
          type: ''
        }
      },
      handleImgSuccess(res, file) {
        this.$message({ message: res.msg, type: 'success' })
        this.form.img = res.data
      },
      // 上传验证
      beforeImgUpload(file) {
        const isJPG =
          file.type === 'image/jpeg' ||
          file.type === 'image/bmp' ||
          file.type === 'image/png'
        const isLtnM = file.size / 1024 / 1024 < 10
        if (!isJPG) {
          this.$message.error('上传图片只能是 JPG,BMP,PNG 格式!')
        }
        if (!isLtnM) {
          this.$message.error('上传图片大小不能超过 10MB!')
        }
        return isJPG && isLtnM
      }
    }
  }
</script>
<style lang="scss" scoped>
  .dialog-footer {
    margin: 15px 0 15px 0;
    width: 175px;
    float: right;
  }
</style>
